package com.zbbt.sportslottery.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.umeng.socialize.sso.UMSsoHandler;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.UpdateVersion;
import com.zbbt.sportslottery.ui.widget.FragmentTabHost;
import com.zbbt.sportslottery.util.KeyUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.UmengShare;
import com.zbbt.sportslottery.util.UmengUtil;

public class MainActivity extends SimpleBaseActivity {
	private long clickTime = 0;
	public static Fragment newFragment;
	private String mTitle[] = new String[] { "首页", "资讯", "活动", "发现", "我的" };
	// 定义数组来存放按钮图片
	private int mImageViewArray[] = { R.drawable.tab_home_selector, R.drawable.tab_news_selector, R.drawable.tab_activity_selector, R.drawable.tab_find_selector, R.drawable.tab_own_selector };
	// 定义FragmentTabHost对象
	private FragmentTabHost mTabHost;
	// 定义一个布局
	private LayoutInflater layoutInflater;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main);
	}

	@Override
	protected void initView() {
		// 实例化布局对象
		layoutInflater = LayoutInflater.from(this);
		// 实例化TabHost对象，得到TabHost
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
		KeyUtil.initFragment();
		int count = mTitle.length;
		for (int postion = 0; postion < count; postion++) {
			TabSpec tabSpec = mTabHost.newTabSpec(mTitle[postion]).setIndicator(getTabItemView(postion));
			// 将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, KeyUtil.getFragment(postion).getClass(), null);
		}
		mTabHost.getTabWidget().setDividerDrawable(null);
		mTabHost.getTabWidget().setBackgroundResource(R.drawable.img_tab_back);
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if (tabId.equals("首页")) {
					UmengUtil.onEvent(context, Constant.UMENG_TABHOST_HOME);
				} else if (tabId.equals("资讯")) {
					UmengUtil.onEvent(context, Constant.UMENG_TABHOST_NEWS);
				} else if (tabId.equals("活动")) {
					UmengUtil.onEvent(context, Constant.UMENG_TABHOST_ACTIVITY);
				} else if (tabId.equals("发现")) {
					UmengUtil.onEvent(context, Constant.UMENG_TABHOST_FIND);
				} else if (tabId.equals("我的")) {
					UmengUtil.onEvent(context, Constant.UMENG_TABHOST_OWN);
				}

			}
		});
		// 检测版本更新
		UpdateVersion updateVersion = new UpdateVersion(context);
		updateVersion.checkUpdate(UpdateVersion.PAGE_MAIN);
	}

	/**
	 * 给Tab按钮设置图标和文字
	 */
	private View getTabItemView(int index) {
		View view = layoutInflater.inflate(R.layout.item_tab_view, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		imageView.setImageResource(mImageViewArray[index]);
		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(mTitle[index]);
		textView.setTextColor(getResources().getColorStateList(R.color.selector_color_green_to_grey ));
		return view;
	}

	@Override
	protected void setListener() {}

	@Override
	protected void initData() {}

	@Override
	protected void onClickEvent(View v) {}

	/**
	 * 全局跳转
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		try {
			if (intent != null) {
				int typeNum = intent.getIntExtra(KeyUtil.GotoType, 0);
				if (typeNum == KeyUtil.GOTO_ACTION) {
					Intent intent1 = new Intent(this, (Class<?>) intent.getSerializableExtra(KeyUtil.ActionKey));
					startActivity(intent1);
				} else if (typeNum == KeyUtil.GOTO_FRAGMENT) {
					changeFragment(intent.getIntExtra(KeyUtil.FragmentKey, 0));
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @MethodName:changeFragment
	 * @Description: 切换Fragment
	 * @param num
	 *            要跳转到的fragment下标
	 * @param isShowDrawer
	 *            是否打开左划栏
	 */

	public void changeFragment(int index) {
		mTabHost.setCurrentTab(index);
		mTabHost.setTag(index);
		// setTitle(mTitle[index]);
	}

	@Override
	public void onBackPressed() {
		if (System.currentTimeMillis() - clickTime < 2000) {
			this.finish();
			App.isLogin = false;
			KeyUtil.getFragments().clear();
		} else {
			ToastUtil.showLong("再点一次退出应用");
			changeFragment(KeyUtil.FRAG_HOME);
		}
		clickTime = System.currentTimeMillis();
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = UmengShare.mController.getConfig().getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}
}
